package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     *
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        this.cartService.addCart(cart);
        return ResponseEntity.ok().build();
    }

    @PostMapping("merge")
    public ResponseEntity<Void> mergeCart(@RequestBody List<Cart> carts) {
        this.cartService.mergeCart(carts);
        return ResponseEntity.ok().build();
    }

    @GetMapping
    public ResponseEntity<List<Cart>> queryCarts() {
        List<Cart> carts=this.cartService.queryCarts();
        if (carts == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(carts);
    }


    @PutMapping
    public ResponseEntity<Void> updateNum(@RequestParam("skuId") Long skuId,
                                          @RequestParam("num") Integer num) {
        this.cartService.updateNum(skuId, num);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") String skuId) {
        System.out.println(skuId);

        this.cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }

    /**
     * 添加到我的关注
     * @param
     * @return
     */
    @PostMapping("/addCollect")
    public ResponseEntity<Void> addCollect(@RequestBody Cart cart){
        this.cartService.addCollect(cart);
        return ResponseEntity.ok().build();
    }

    /**
     * 将选中的商品添加到我的关注
     * @param carts
     * @return
     */
    @PostMapping("addCollect/all")
    public ResponseEntity<Void> addCollectAll(@RequestBody List<Cart> carts){
        this.cartService.addCollectAll(carts);
        return ResponseEntity.ok().build();
    }
}
